package com.buer.storage.api;

import com.buer.storage.business.domain.UserEntity;
import com.buer.storage.business.dto.LoginDTO;
import com.buer.storage.business.service.UserEntityService;
import com.buer.storage.common.annotation.Login;
import com.buer.storage.common.constant.Constant;
import com.buer.storage.common.eumn.ResultEnum;
import com.buer.storage.common.handle.BaseException;
import com.buer.storage.common.holder.LoginUser;
import com.buer.storage.common.holder.RequestTypeHolder;
import com.buer.storage.common.utils.BaseResult;
import com.buer.storage.common.utils.CheckEmptyUtil;
import com.buer.storage.common.utils.MD5Utils;
import com.buer.storage.common.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 登录校验
 */
@RestController("/")
@Api(value = "登录相关", description = "登录相关")
public class AuthController {

    private static final Logger logger = LoggerFactory.getLogger(AuthController.class);

    @Autowired
    UserEntityService userEntityService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    @Login
    public BaseResult login(@RequestBody LoginDTO loginDTO) {

        if (CheckEmptyUtil.isEmpty(loginDTO.getUsername())) {
            return BaseResult.build(ResultEnum.USERNAME_NULL);
        }

        if (CheckEmptyUtil.isEmpty(loginDTO.getPassword())) {
            return BaseResult.build(ResultEnum.PASSWORD_NULL);
        }

        Map map = new HashMap<>();
        map.put("username", loginDTO.getUsername());
        UserEntity userDO = userEntityService.getOne(map);
        if (userDO == null) {
            return BaseResult.build(ResultEnum.NO_EXIT_USER);
        }

        if (Objects.equals(userDO.getStatus(), 0)) {
            return BaseResult.build(ResultEnum.USER_FORBIDDEN);
        }

        String encryptPass = MD5Utils.encrypt(loginDTO.getUsername(), loginDTO.getPassword());

        if (!Objects.equals(userDO.getPassword(), encryptPass)) {
            return BaseResult.build(ResultEnum.ERROR_PASSWORD);
        }
        String token = MD5Utils.encrypt(loginDTO.getUsername());

        try {
            String requestTypeHolder = RequestTypeHolder.getRequestTypeHolder();

            LoginUser loginUser = new LoginUser();
            loginUser.setUserId(userDO.getUserId());
            loginUser.setUserName(userDO.getUsername());
            loginUser.setName(userDO.getName());
            loginUser.setType(userDO.getType());
            loginUser.setRequestType(requestTypeHolder);
            loginUser.setToken(token);

            String key = requestTypeHolder + ":" + "token" + ":" + token;
            RedisUtil.set(key, loginUser, Constant.REDIS_EXPIRE);

            //存储user
            String userKey = Constant.LOGINUSER + ":" + loginUser.getUserId();
            RedisUtil.set(userKey, userDO);

        } catch (Exception e) {
            logger.error("redis连接异常");
            throw new BaseException(ResultEnum.REDIS_SERVER_ERROR);
        }

        Map result = new HashMap();
        result.put("token", token);

        return BaseResult.build(ResultEnum.SUCCESS, result);

    }
}